package com.yxp.offer;

/**
 * @author : yang.xp
 * @date : 2021/8/17 9:26
 * @description : 剑指 Offer 11. 旋转数组的最小数字;见到已经排序数组内查找，就应该想到二分查找
 */
public class Problem11 {
    public static void main(String[] args) {
        int[] numbers = new int[]{3, 4, 5, 6, 7};
        Problem11 problem11 = new Problem11();
        System.out.println(problem11.minArray(numbers));
        System.out.println(problem11.minArray2(numbers));
    }

    public int minArray(int[] numbers) {
        int minNum = -1;
        if (numbers == null) {
            return minNum;
        }
        for (int i = 0; i < numbers.length - 1; i++) {
            if (numbers[i + 1] < numbers[i]) {
                minNum = numbers[i + 1];
                break;
            }
        }
        if (minNum == -1) {
            return numbers[0];
        }
        return minNum;
    }

    public int minArray2(int[] numbers){
        int left = 0, right = numbers.length - 1;
        while(left < right) {
            int mid = (right - left) / 2 + left;
            if(numbers[right] > numbers[mid]) right = mid;
            else if(numbers[right] < numbers[mid]) left = mid + 1;
            else right --;
        }
        return numbers[left];
    }
}
